package myutil;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.util.Version;

public class MyStemmer {
	Reader docreader;
	Analyzer az = new StandardAnalyzer(Version.LUCENE_40);

	public MyStemmer() {
		// TODO Auto-generated constructor stub

	}

	public MyStemmer(Reader reader) {
		// TODO Auto-generated constructor stub
		this.docreader = reader;
	}

	public MyStemmer(Reader reader, Analyzer az) {
		//specify another analyzer
		// TODO Auto-generated constructor stub
		this.docreader = reader;
		this.az = az;
	}

	/**
	 * @param args
	 * @throws IOException
	 */
	public void std_std_test() throws IOException {
		String s = "You need four JARs: the Lucene JAR, the queryparser JAR, the common analysis JAR, and the Lucene demo JAR. You should see the Lucene JAR file in the core/ directory you created when you extracted the archive -- it should be named something like lucene-core-{version}.jar. You should also see files called lucene-queryparser-{version}.jar, lucene-analyzers-common-{version}.jar and lucene-demo-{version}.jar under queryparser, analysis/common/ and demo/, respectively.";
		StringReader reader = new StringReader(s);
		TokenStream ts = az.tokenStream("feild", reader);
		boolean hasnext = ts.incrementToken();
		CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
		while (hasnext) {
			System.out.println(termAtt);
			hasnext = ts.incrementToken();
		}
		az.close();
	}

	public TokenStream std_std() {
		try {
			TokenStream ts = az.tokenStream("feild", this.docreader);
			az.close();
			return ts;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public Analyzer GetAnalyzer() {
		return this.az;
	}

	public Reader GetReader() {
		return this.docreader;
	}
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		MyStemmer ms = new MyStemmer();
		ms.std_std_test();
	}

}
